package com.gitee.sop.layui.adminserver.module.background.system.api.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.sop.layui.adminserver.module.background.system.api.service.param.InstanceMonitorSearchParam;
import com.gitee.sop.layui.adminserver.module.background.system.api.service.param.MonitorErrorMsgParam;
import com.gitee.sop.layui.adminserver.module.background.system.api.service.param.MonitorInfoErrorSolveParam;
import com.gitee.sop.layui.adminserver.module.background.system.api.service.param.MonitorSearchParam;
import com.gitee.sop.layui.adminserver.module.background.system.bean.RouteErrorCount;
import com.gitee.sop.layui.adminserver.module.background.system.entity.MonitorInfoError;
import com.gitee.sop.layui.adminserver.module.background.system.entity.MonitorSummary;
import com.gitee.sop.layui.adminserver.module.background.system.mapper.MonitorInfoErrorMapper;
import com.gitee.sop.layui.adminserver.module.background.system.mapper.MonitorInfoMapper;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 路由监控
 *
 * @author admin
 */
@RestController
public class MonitorNewApi {

    @Autowired
    private MonitorInfoMapper monitorInfoMapper;

    @Autowired
    private MonitorInfoErrorMapper monitorInfoErrorMapper;

    @GetMapping("/api/monitornew.data.page")
    Result listMonitor(MonitorSearchParam param) {
        Page<MonitorSummary> page = new Page<>(param.getPageIndex(), param.getPageSize());
        List<MonitorSummary> monitorSummaries = monitorInfoMapper.listMonitorSummary(param, page);
        page.setRecords(monitorSummaries);
        return Result.successPage(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("/api/monitornew.routeid.data.get")
    Result listInstanceMonitor(InstanceMonitorSearchParam param) {
        List<MonitorSummary> monitorSummaries = monitorInfoMapper.listInstanceMonitorInfo(param);
        return Result.success(monitorSummaries);
    }

    private Map<String, Integer> getRouteErrorCount() {
        List<RouteErrorCount> routeErrorCounts = monitorInfoErrorMapper.listRouteErrorCount();
        return routeErrorCounts.stream()
                .collect(Collectors.toMap(RouteErrorCount::getRouteId, RouteErrorCount::getCount));
    }

    @GetMapping("/api/monitornew.error.page")
    Result listError(MonitorErrorMsgParam param) {
        Page<MonitorInfoError> page = new Page<>(param.getPageIndex(), param.getPageSize());
        LambdaQueryWrapper<MonitorInfoError> wrapper = new QueryWrapper<MonitorInfoError>().lambda()
                .eq(MonitorInfoError::getRouteId, param.getRouteId())
                .eq(MonitorInfoError::getIsDeleted, 0)
                .orderByDesc(MonitorInfoError::getGmtModified);
        monitorInfoErrorMapper.selectPage(page, wrapper);
        return Result.successPage(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    @PostMapping("/api/monitornew.error.solve")
    Result solve(MonitorInfoErrorSolveParam param) {
        MonitorInfoError monitorInfoError = new MonitorInfoError();
        monitorInfoError.setIsDeleted((byte) 1);
        monitorInfoError.setCount(0);
        monitorInfoErrorMapper.update(monitorInfoError, new UpdateWrapper<MonitorInfoError>().lambda().eq(MonitorInfoError::getErrorId, param.getErrorId()));
        return Result.success();
    }

}
